<template>
  <a-modal
    :title="title"
    :width="720"
    :visible="visible"
    :confirmLoading="confirmLoading"
    :maskClosable="false"
    :destroyOnClose="true"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item
          label="题目标题"
          :label-col="labelCol"
          :wrapper-col="wrapperCol">
          <a-input
            allowClear
            placeholder="请输入题目标题"
            v-decorator="['title', {rules: [{required: true, message: '请输入题目标题'}]}]"
          />
        </a-form-item>
        <a-form-item label="题目类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-radio-group
            v-decorator="['qt_type', {initialValue:1,rules: [{required: true}]}]"
          >
            <a-radio :value="1">
              判断题
            </a-radio>
            <a-radio :value="2">
              单选题
            </a-radio>
            <a-radio :value="3">
              多选题
            </a-radio>
          </a-radio-group>
        </a-form-item>
        <a-form-item label="题目选项" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-button type="primary" size="small" @click="addQtItem">增加一项</a-button>
          <table class="table" border="1">
            <tr style="font-weight: bold">
              <td>标识</td>
              <td>选项名称</td>
              <td>操作</td>
            </tr>
            <tr v-for="(item,index) in qt_item">
              <td>
                <a-input
                  v-model="item.item"
                  placeholder="请输入标识"
                />
              </td>
              <td>
                <a-input
                  v-model="item.value"
                  placeholder="请输入选项名称"
                />
              </td>
              <td><a style="color:red" @click="delQtItem(index)">删除</a></td>
            </tr>
          </table>
        </a-form-item>
        <a-form-item
          label="正确答案"
          :label-col="labelCol"
          :wrapper-col="wrapperCol"
          extra="输入题目选项中的标识。单选直接填写一个标识，示例：A；多选则使用,隔开示例：A,B,C"
        >
          <a-input
            allowClear
            placeholder="请输入正确答案"
            v-decorator="['answer', {rules: [{required: true, message: '请输入正确答案'}]}]"
          />
        </a-form-item>

        <a-form-item
          label="题目分数"
          :label-col="labelCol"
          :wrapper-col="wrapperCol">
          <a-input
            allowClear
            placeholder="请输入题目分数"
            v-decorator="['score', {rules: [{required: true, message: '请输入题目分数'}]}]"
          />
        </a-form-item>

        <a-form-item label="状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-radio-group
            v-decorator="['status', {initialValue:0}]"
          >
            <a-radio :value="0">
              启用
            </a-radio>
            <a-radio :value="1">
              停用
            </a-radio>
          </a-radio-group>
        </a-form-item>

      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import * as Api from '@/api/question/topic'
import _ from 'lodash'

export default {
  components: {},
  props: ['questionId'],
  data () {
    return {
      // 对话框标题
      title: '',
      // 标签布局属性
      labelCol: {
        span: 7
      },
      // 输入框布局属性
      wrapperCol: {
        span: 13
      },
      // modal(对话框)是否可见
      visible: false,
      // modal(对话框)确定按钮 loading
      confirmLoading: false,
      // 当前表单元素
      form: this.$form.createForm(this),
      // 当前记录
      record: {},
      // 当前模式 add新增 edit编辑
      mode: '',

      qt_item: [{
        item: 'A', value: ''
      }],

      z: 'ABCDEFG'
    }
  },
  methods: {
    /**
     * 显示对话框
     */
    add () {
      this.mode = 'add'
      this.title = '增加题目'
      this.visible = true
      this.record = {}
    },
    edit (record) {
      this.mode = 'edit'
      this.title = '编辑题目'
      this.visible = true
      this.record = record
      this.setFieldsValue()
    },

    addQtItem () {
      if (this.qt_item.length >= 7) {
        this.$message.error('已达题目选择上限！', 1.5)
        return
      }
      const items = this.z.split('')
      this.qt_item.push({
        item: items[this.qt_item.length], value: ''
      })
    },

    delQtItem (index) {
      if (this.qt_item.length <= 1) {
        this.$message.error('至少保留一项！', 1.5)
        return
      }
      this.qt_item.splice(index, 1)
    },

    /**
     * 设置默认值
     */
    setFieldsValue () {
      const { form: { setFieldsValue } } = this
      this.$nextTick(() => {
        const data = _.pick(this.record, ['title', 'qt_type', 'answer', 'score', 'status'])
        setFieldsValue(data)
        this.qt_item = this.record.qt_item
      })
    },

    /**
     * 确认按钮
     */
    handleSubmit (e) {
      e.preventDefault()
      const { form: { validateFields } } = this
      // 表单验证
      validateFields((errors, values) => {
        // 提交到后端api
        !errors && this.onFormSubmit(values)
      })
    },

    /**
     * 关闭对话框事件
     */
    handleCancel () {
      this.visible = false
      this.form.resetFields()
    },

    /**
     * 提交到后端api
     */
    onFormSubmit (values) {
      this.confirmLoading = true
      var sendApi = null

      values.qt_item = this.qt_item

      if (this.mode == 'add') {
        values.question_id = this.question_id
        sendApi = Api.add({ form: values })
      } else {
        sendApi = Api.edit({ apiId: this.record['qt_id'], form: values })
      }
      sendApi.then((result) => {
        // 显示成功
        this.$message.success(result.message, 1.5)
        // 关闭对话框
        this.handleCancel()
        // 通知父端组件提交完成了
        this.$emit('handleSubmit', values)
      })
        .finally((result) => {
          this.confirmLoading = false
        })
    }

  }
}
</script>
<style scoped lang="less">
.table {
  width: 100%;
  border-color: #e8e8e8;
  text-align: center;

  td {
    width: 33.333%;
  }
}
</style>
